package com.skt.common_hr.overtime.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class HolidayProxy extends CommonProxy
	{
		public static const NAME:String = "HolidayProxy";

		public static const LOAD_HOLIDAYS_COMPLETE:String = "loadHolidayComplete";
		public static const LOAD_HOLIDAYS_FAILED:String = "loadHolidayFailed";

		public var list:Array = [];

		public function HolidayProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function load(strMonth:String = ""):void
		{
			var param:Object = new Object();
			var year:int = parseInt(strMonth.substr(0, 4));
			var month:int = parseInt(strMonth.substr(4));

			var beg:Date = new Date(year, month-1, 1);
			var end:Date = new Date(beg);
			end.month += 1;
			end.date -= 1;

			param.I_FROM = DateUtil.toBasicString(beg);
			param.I_TO = DateUtil.toBasicString(end);
			
			loadData("Z_HR_0112", param, holidayResult, holidayFault);
		}
		
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 		- 공휴일
		 * @param
		 * @return void
		 */
		public function holidayResult(event:ResultEvent):void
		{
			if(retCode==0) {  
				list = parseHolidays(event.result.CALTAB);
				facade.sendNotification(LOAD_HOLIDAYS_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_HOLIDAYS_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 		- 경조사유콤보
		 * @param
		 * @return void
		 */
		private function holidayFault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_HOLIDAYS_FAILED, this);
		}
		
		
		private function parseHolidays(srcArr:ArrayCollection):Array
		{
			var retArr:Array = new Array();
			for each(var rec:Object in srcArr) 
			{
				var holiday:Date = DateUtil.stringToDateTime(rec.DATUM);
				if(holiday.day >0 && holiday.day <6) retArr.push(holiday);
			}
			return retArr;	
		}
	}
}